<template>
  <el-container>
    <el-header>
      <div>
        <img src alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!--侧边栏菜单-->
        <el-menu
          :collapse="isCollapse"
          unique-opened
          background-color="#323744"
          text-color="#fff"
          active-text-color="#369BFF"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <el-submenu :index="item.id+''" v-for="(item) in menulist" :key="item.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item
              :index="'/'+subitem.path"
              v-for="subitem in item.children"
              :key="subitem.id"
              @click="saveNavActive('/'+subitem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getMenus } from '../../network/menu.js'
export default {
  created() {
    this.getMenuList()
    this.activePath = sessionStorage.getItem('activePath')
  },
  data() {
    return {
      menulist: [],
      isCollapse: false,
      activePath: ''
    }
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有数据
    async getMenuList() {
      const { data: res } = await getMenus()

      if (res.meta.status !== 200) return this.$message(res.meta.msg)
      this.menulist = res.data
    },

    // 切换菜单的折叠和展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },

    // 设置链接的激活
    saveNavActive(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang='less' scoped>
.el-container {
  height: 100%;
}
.el-header {
  background: #373c41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #ffffff;
  font-size: 20px;
  display: flex;
  align-items: center;
}

.el-aside {
  background: #323744;
  .el-menu {
    border-right: 0;
  }
}
.el-main {
  background: #eaedf2;
}

.toggle-button {
  font-size: 10px;
  background: #485164;
  text-align: center;
  color: #ffffff;
  line-height: 24px;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
